package com.luoyulin.linkedlist;

/**
 * @author luoyulin
 * @create 2022-01-12 23:05
 **/
public class DoubleLinkedListDemo {
    public static void main(String[] args) {
        System.out.println("双向链表测试");
        HeroNode2 hero1 = new HeroNode2(1, "宋江", "及时雨");
        HeroNode2 hero2 = new HeroNode2(2, "骆鱼鳞", "笑傲可爱");
        HeroNode2 hero3 = new HeroNode2(3, "邱老板", "妹妹");
        HeroNode2 hero4 = new HeroNode2(4, "大哥哥", "帅哥");
        DoubleLinkedList doubleLinkedList = new DoubleLinkedList();
        doubleLinkedList.add(hero1);
        doubleLinkedList.add(hero2);
        doubleLinkedList.add(hero3);
        doubleLinkedList.add(hero4);
        doubleLinkedList.showList();

        //修改
        System.out.println("修改测试：");
        HeroNode2 newHeroNode = new HeroNode2(2, "骆琳琳", "帅哥");
        doubleLinkedList.update(newHeroNode);
        doubleLinkedList.showList();

    }
}

class DoubleLinkedList{
    //初始化一个头节点
    private  HeroNode2 head = new HeroNode2(0,"","");
    //遍历双向链表
    public void showList() {
        if (head.next == null) {
            System.out.println("链表为空");
            return;
        }
        HeroNode2 temp = head.next;
        while (true) {
            if (temp == null) {
                break;
            }
            System.out.println(temp);
            temp = temp.next;
        }
    }
    //添加
    public void add(HeroNode2 heroNode) {
        //辅助变量
        HeroNode2 temp = head;
        while (true) {
            if (temp.next == null) {
                break;
            }
            temp = temp.next;
        }
        //记得加上pre，形成双向链表
        temp.next = heroNode;
        heroNode.pre = temp;
    }

    //修改一个节点的内容
    public void update(HeroNode2 heroNode){
        if(head.next == null){
            System.out.println("链表为空。");
            return;
        }
        HeroNode2 temp = head.next;
        boolean flag = false;
        while (true){
            if(temp == null){
                break;//链表遍历结束
            }
            if(temp.no == heroNode.no){
                //找到了
                flag = true;
                break;
            }
            temp = temp.next;
        }
        //根据flag是否找到修改的节点
        if(flag){
            temp.name = heroNode.name;
            temp.nickName = heroNode.nickName;
        }else{
            System.out.printf("没有找到编号维%d的节点，不能修改\n",heroNode.no);
        }
    }
    //删除一个节点，可以直接找到要删除的节点，自我删除
    public void delet(int no){
        if(head.next == null){
            System.out.println("空链表");
            return;
        }
        HeroNode2 temp = head.next;
        boolean flag = false;
        while (true){
            if(temp == null){
                break;
            }
            if(temp.no == no){
                flag = true;
                break;
            }
            temp = temp.next;
        }
        if(flag){
            temp.pre.next = temp.next;
            //如果是最后一个节点就不需要执行这句话，不然会空指针 因为temp.next = null
            if(temp.next != null) {
                temp.next.pre = temp.pre;
            }
        } else{
            System.out.println("找不到节点，无法删除");
        }
    }

}

//定义一个HeroNode
class HeroNode2 {
    public int no;
    public String name;
    public String nickName;
    public HeroNode2 next;
    public HeroNode2 pre;

    public HeroNode2(int hNo, String hName, String hNickName) {
        this.no = hNo;
        this.name = hName;
        this.nickName = hNickName;
    }

    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                ", nickName='" + nickName + '\'' +
                '}';
    }
}